﻿@model MoviesRoom.ViewModels.Film.FullFilmViewModel

@{
    ViewBag.Title = "Film Details";
    ViewBag.FilmTitle = @Model.Title;
    ViewBag.FilmImage = ((@Model.Image != null) ? @Model.Image : "../../img/Unavailable_Pic.gif");
                                                                              
}

<h2 id="film-details">@ViewBag.Title</h2>

<div class="row-fluid">
    <div class="span4">
        <img src="@((Model.Image != null) ? @Model.Image : "../../img/Unavailable_Pic.gif")"
             alt="@Model.Title" class="film-larger-image" />
        @if (User.Identity.IsAuthenticated)
        {
            if (Model.AvailableTickets > 0)
            {
                using (Html.BeginForm("GenerateTickets", "Ticket", new { filmId = @Model.Id }, FormMethod.Post, new { Id = "ticketCounter" }))
                {
                                    <label for="tb-tickets">Buy Tickets: </label>
                                    <input type="number" max="@Model.AvailableTickets" min="1"
                                           id="tb-tickets"
                                           value="1" name="TicketsCount" />
                                    <input type="submit" value="Buy" class="btn btn-primary" />
                                    @Html.ValidationSummary()
                }
            }
            else
            {
                <h3>Sold out</h3>
            }
        }

        @if (User.IsInRole("admin"))
        {

                    <label for="UploadPicture">Upload new picture: </label>
                    @(Html.Kendo()
                          .Upload()
                          .Name("UploadPicture")
                          .Async(a =>
                          {
                              a.AutoUpload(true);
                              a.Save("UploadPicture", "Home", new { id = Model.Id });
                          })
                          .ShowFileList(false)
                          .Multiple(false)
                          .Events(ev =>
                          {
                              ev.Success("successUpload");
                              ev.Error("errorUpload");
                          }))

                    <div class="span12 text-warning" id="upload-result"></div>
        }
    </div>
    <div class="film-details">
        <strong class="film-title">Title: </strong>
        <div>@Model.Title</div>
        <div class="fb-share-button" data-href="http://moviesroom.apphb.com/Home/SingleFilm/@Model.Id" data-width="60px" data-type="button_count"></div>
    </div>
    <div class="film-details">
        <strong class="film-title">Category: </strong>
        <div>@Model.Category.Name</div>
    </div>
    <div class="film-details">
        <strong class="film-title">Description: </strong>
        <p class="film-description">
            @if (@Model.Description != null)
            {
                            @Model.Description
            }
            else
            {
                <span>No Description</span>
            }

        </p>
    </div>
    <div class="film-details">
        <strong class="film-title">Available Tickets: </strong>
        <div>@Model.AvailableTickets</div>
    </div>
    <div class="film-details">
        <strong class="film-title">Ticket Price: </strong>
        <div>€@Model.PricePerTicket</div>
    </div>
    <div class="film-details">
        <strong class="film-title">Start Date: </strong>
        <div>@String.Format("{0:dd/MMM/yyyy HH:mm}", Model.StartDate)</div>
    </div>
    <div class="film-details">
        <strong class="film-rating">Rating:</strong>
        <div class="single-film-rating">
            @if (@Model.VoteAsString != null)
            {
                            <span> @Model.VoteAsString </span>
            }
            else
            {
                <span>0.0</span>
            }
        </div>
        @if (User.Identity.IsAuthenticated && @Model.CanVote)
        {
            using (Ajax.BeginForm("Vote", "Votes",
                new
            {
                FilmId = @Model.Id
            },
                new AjaxOptions
                {
                    HttpMethod = "POST",
                    OnSuccess = "votingSuccesfull",
                    OnFailure = "votingError"
                }, new { Id = "vote-list" }))
            {

                            @Html.DropDownList("Value", new List<SelectListItem>
                            {
                                new SelectListItem { Text = "1", Value = "1" },
                                new SelectListItem { Text = "2", Value = "2" },
                                new SelectListItem { Text = "3", Value = "3" },
                                new SelectListItem { Text = "4", Value = "4" },
                                new SelectListItem { Text = "5", Value = "5" },
                                new SelectListItem { Text = "6", Value = "6" },
                                new SelectListItem { Text = "7", Value = "7" },
                                new SelectListItem { Text = "8", Value = "8" },
                                new SelectListItem { Text = "9", Value = "9" },
                                new SelectListItem { Text = "10", Value = "10" }
                            }, new { Id = "rating-list" })
                            <input type="submit" value="Vote" id="vote" class="btn btn-primary" />

            }
        }

    </div>
    <hr />
    <div class="add-comment">
        @if (User.Identity.IsAuthenticated)
        {
            using (Ajax.BeginForm("AddComment",
                new
            {
                FilmId = @Model.Id,
                Author = User.Identity.Name
            },
                new AjaxOptions
                {
                    HttpMethod = "POST",
                    UpdateTargetId = "updatater",
                    InsertionMode = InsertionMode.Replace,
                    OnSuccess = "clearCommentBox",
                    OnFailure = "addCommentError"
                }))
            {
                <div id="comment-holder">
                    <input type="text" id="comment-text-box" placeholder="Leave a comment" />
                </div>
            }
        }
    </div>
    <div id="updatater">
        @Html.Partial("_CommentsPartial", Model.Comments)
    </div>
   
</div>


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

@section Kendo {
    @Styles.Render("~/Content/Kendo/css")
    @Scripts.Render("~/bundles/kendo")
}

<script type="text/javascript">
    function successUpload(ev) {
        window.location.reload();
    }

    function errorUpload(ev) {
        $('#upload-result').show(500).text("Upload file must be a image and max 1MB");
        setTimeout(function () {
            $('#upload-result').hide(500);
        }, 7000);
    }

    function clearCommentBox() {
        $("#comment-box").val("");
    }

    function votingSuccesfull(result) {
        $("#vote-list").remove();
        $(".single-film-rating").after("<span id='vote-success'>Your vote was recorded.</span>")
        $(".single-film-rating span").html(result);
    }

    function votingError(err) {
        $("#vote-list").remove();
        $(".single-film-rating").after("<span id='vote-error'>" + err.statusText + "</span>")
    }

    function addCommentError(err){
        $("#vote-error").remove();
        $(".add-comment").prepend("<span id='vote-error'>" + err.statusText + "</span>");
    }

    $(function () {
        $("#comment-text-box").focus(function () {
            $("#comment-holder").html('<textarea id="comment-box" name="Text" maxlength="400" placeholder="Leave a comment"></textarea><input type="submit" value="Submit" id="submit-comment" class="btn btn-primary" />')
            $("#comment-box").show('slow');
            $("#submit-comment").show('slow');
        });
    });

</script>